package com.aia.ann.web.xml;

import java.io.File;
import java.util.Map;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.apache.poi.util.IOUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.aia.ann.domain.Agent;
import com.aia.ann.service.ChiefService;
import com.cfh.util.Tools;
import com.opensymphony.xwork2.ActionSupport;

public class AnnUploadXml2 extends ActionSupport implements SessionAware {
	private static final Logger logger = Logger.getLogger(AnnUploadXml2.class);
	private static final long serialVersionUID = 1L;
	private Map session;
//	private File imgcode;
	//fileUpload1有字，fileUpload2没字
	private File fileUpload1;
	private File fileUpload2;
//	private String imgcodeFileName;
	private String fileUpload1FileName;
	private String fileUpload2FileName;
	private ChiefService chiefService;
	private String xml;

	public void setSession(Map session) {
		this.session = session;
	}

	public Map getSession() {
		return session;
	}

	public void setChiefService(ChiefService chiefService) {
		this.chiefService = chiefService;
	}

	public ChiefService getChiefService() {
		return chiefService;
	}

	public void validate() {
		logger.debug("imageFileName:" + fileUpload1FileName);
		logger.debug("fileUpload is null?:" + ((fileUpload1 != null) ? "not null" : "null"));
		if (fileUpload1 == null) {
			this.addActionError("fileUpload1 is null");
			return;
		}
		
		if (fileUpload2 == null) {
			this.addActionError("fileUpload2 is null");
			return;
		}

		if (fileUpload1.length() > 1024 * 1024) {
			this.addActionError("您选择的文件1大小超过了1024KB");
			return;
		}
		
		if (fileUpload2.length() > 1024 * 1024) {
			this.addActionError("您选择的文件2大小超过了1024KB");
			return;
		}
	}

	public String execute() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("AnnUploadXml - start"); //$NON-NLS-1$
		}
		
		
		
	
		try {
			FileUtils.copyFile(fileUpload1, new File("D:/fileUpload1_annv2.png"));
			FileUtils.copyFile(fileUpload2, new File("D:/fileUpload2_annv2.png"));
		} catch (Exception e) {
		 logger.warn("Cannot save img to D:/annv2.png", e);
		}
		

		Agent cust = null;
		try {
			cust = (Agent) session.get("customer");
			if (cust == null) {
				addActionError("没有登录或会话超时");
				return LOGIN;
			}
		} catch (Exception e) {
			logger.error("Unknown Error:" + e, e);
			throw new RuntimeException(e);
		}

		try {
			if (logger.isDebugEnabled()) {
				logger.debug("FileUtils.copyFile(imgcode, new File(D:/a.jpg));"); //$NON-NLS-1$
			}
//			try {
//				FileUtils.copyFile(imgcode, new File("D:/ann.jpg"));
//			} catch (Exception e) {
//				logger.warn("Unknown Error:" + e, e);
//			}

			chiefService.annUploadXml2(this);

			// xml="<?xml version=\"1.0\" encoding=\"utf-8\"?><data>"+cust.getAG_USERID()+"</data>";

			if (logger.isDebugEnabled()) {
				logger.debug("AnnUploadXml - end"); //$NON-NLS-1$
			}

			return Tools.smartReturn((ActionSupport) this);
		} catch (Exception e) {
			logger.error("Unknown Error:" + e, e);
			throw new RuntimeException(e);
		}
	}

	public void setXml(String xml) {
		this.xml = xml;
	}

	public String getXml() {
		return xml;
	}


	public void setFileUpload1(File fileUpload) {
		this.fileUpload1 = fileUpload;
	}

	public File getFileUpload1() {
		return fileUpload1;
	}

	public void setFileUpload1FileName(String fileUpload1FileName) {
		this.fileUpload1FileName = fileUpload1FileName;
	}

	public String getFileUpload1FileName() {
		return fileUpload1FileName;
	}

	public void setFileUpload2(File fileUpload2) {
		this.fileUpload2 = fileUpload2;
	}

	public File getFileUpload2() {
		return fileUpload2;
	}

	public void setFileUpload2FileName(String fileUpload2FileName) {
		this.fileUpload2FileName = fileUpload2FileName;
	}

	public String getFileUpload2FileName() {
		return fileUpload2FileName;
	}

}
